package com.android.happymall.fragment.group;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.happymall.R;
import com.android.happymall.fragment.BaseLazyFragment;
import com.android.happymall.fragment.detail.Fragment_DirectBuy_TabList;
import com.android.happymall.model.group.DirectBuyData;
import com.android.happymall.model.group.DirectBuyDataCallback;
import com.android.happymall.utils.ConstantsUrl;
import com.android.happymall.utils.HttpUtils;
import com.android.happymall.utils.ObjectUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;

/**
 * 直购商品主页
 */

public class Fragment_DirectBuy extends BaseLazyFragment implements BaseQuickAdapter.RequestLoadMoreListener {
    @BindView(R.id.no_data)
    RelativeLayout noData;
    @BindView(R.id.no_data_tv)
    TextView noDataTv;
    @BindView(R.id.progress)
    RelativeLayout progress;
    @BindView(R.id.click_resetnetwork)
    LinearLayout clickResetnetwork;
    @BindView(R.id.tab)
    TabLayout tabLayout;
    @BindView(R.id.pager)
    ViewPager viewPager;
    private static final String ARG_PARAM1 = "param1";
    private LayoutInflater inflater;
    private View view;
    private FragmentActivity mcontext;
    private boolean networkConnected;
    private GroupFragmentPagerAdapters pagerAdapter;
    private StaggeredGridLayoutManager mLayoutManager;
    private ArrayList<DirectBuyData.DataBean> cat_list = new ArrayList<>();
    private int page = 1;

    public static Fragment_DirectBuy newInstance() {
        Fragment_DirectBuy fragment = new Fragment_DirectBuy();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
        }
    }

    @Override
    protected View initView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        this.inflater = inflater;
        view = inflater.inflate(R.layout.fragment_directbuy, container, false);
        mcontext = getActivity();
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    protected void initPrepare() {
        initUI();
    }

    @Override
    protected void onInvisible() {

    }

    @Override
    protected void initData() {
        GetBDMain();
    }

    @Override
    public void onLoadMoreRequested() {
        page = page + 1;
        GetBDMain();
    }

    private void initUI() {

    }

    private class GroupFragmentPagerAdapters extends FragmentPagerAdapter {

        public GroupFragmentPagerAdapters(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return Fragment_DirectBuy_TabList.newInstance(position, cat_list.get(position).getId());
        }

        @Override
        public int getCount() {
            return cat_list.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return cat_list.get(position).getCatname();
        }
    }

    private void GetBDMain() {
        networkConnected = ObjectUtils.isNetworkConnected(mcontext);
        String address = ConstantsUrl.domain + ConstantsUrl.MAIN_GOODSCATEGORYDATA;
        HashMap<String, String> map = new HashMap<>();
        map.put("page", page + "");
        if (networkConnected) {

            HttpUtils.get(address, map, dataCallback);
        } else {
            if (page == 1) {
                ObjectUtils.GetDataNet(clickResetnetwork, progress, 2);
            }
            ObjectUtils.toast(mcontext, "网络未连接");
        }
    }

    DirectBuyDataCallback dataCallback = new DirectBuyDataCallback() {

        @Override
        public void onError(Call call, Exception e, int id) {
            ObjectUtils.GetDataNet(clickResetnetwork, progress, 2);
        }

        @Override
        public void onResponse(DirectBuyData response, int id) {
            ObjectUtils.GetDataNet(clickResetnetwork, progress, 1);

            if (response.getCode() == 0) {
                if (response.getData() == null && page == 1) {
                    noData.setVisibility(View.VISIBLE);
                    return;
                }
                if (response.getData() != null) {
                    cat_list.clear();
                    cat_list.add(new DirectBuyData.DataBean("0", "全部", "", "", "", "", ""));
                    cat_list.addAll(response.getData());
                    if (response.getData() != null && response.getData().size() > 0) {
                        pagerAdapter = new GroupFragmentPagerAdapters(mcontext.getSupportFragmentManager());
                        viewPager.setAdapter(pagerAdapter);
                        tabLayout.setupWithViewPager(viewPager);
                    }
                }
            } else {
                noData.setVisibility(View.VISIBLE);
                noDataTv.setVisibility(View.VISIBLE);
                noDataTv.setText(response.getMsg());
            }
        }
    };
}
